import $axios from "@/common/http.js";//请求实例
import { Message } from "element-ui";
import router from "@/router"


/**
 * 
 * @returns 角色列表
 */
export const getRolelist = async () => {
    let { list } = await $axios.get('/rolelist')
    return list
}

/**
 * 
 * @param {*} data 角色添加  
 *  title 角色名  menus  菜单权限   status 状态
 */
export const addRolelist = async (data) => {
    // console.log(title, menus, status);
    let { title, menus, status } = data
    let rst = await $axios.post('/roleadd', { rolename: title, menus, status })
    // console.log(rst);
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
            onClose() {
                router.back()
            }
        })
    }
}

/**
 * 
 * @param {* 角色id } id   删除角色
 * @returns 
 */

export const delRolelist = async (id) => {
    let rst = await $axios.post('/roledelete', { id })
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
        })
        return true
    } else {
        return false
    }
}


/**
 * 
 * @param {*角色id} id   修改status 启用不启用
 */

export const modRolelist = async (data) => {
    // console.log(data);
    let { id, menus, rolename, status } = data
    // status = status == 0 ? 1 : 0
    // console.log(id, menus, rolename, status);
    let rst = await $axios.post('/roleedit', { id, menus, rolename, status })
    // console.log(rst);
    if (rst.code == 200) {
        Message({
            type: 'success',
            message: rst.msg,
            duration: 500,
        })
        return true
    } else {
        return false
    }
}

/**
 * 
 * @param {*} id   根据id查一条
 */

export const modonceRolelist = async (id) => {
    let rst = await $axios.get('/roleinfo?id=' + id)
    // console.log(rst);
    return rst.list
}